<template>
    <div class="login">
        <message ref="message"></message>
        <div class="header">
            <img src="../../img/logo.gif"><span>欢迎啊，怪兽！</span>
        </div>
        <div class="content">
            <el-form :model="login" :rules="rules" ref="loginForm" class="ruleForm">
                <el-form-item prop="account">
                    <el-input v-model="login.account" placeholder="账号" auto-complete="off">
                        <template slot="prepend"><i class="el-icon-fa-user-circle-o"></i></template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" v-model="login.password" placeholder="密码" auto-complete="off">
                        <template slot="prepend"><i class="el-icon-fa-expeditedssl"></i></template>
                    </el-input>
                </el-form-item>
            </el-form>
        </div>
        <div class="operator">
            <el-button type="primary" @click="handleLogin">登录</el-button>
        </div>
        <div class="footer">
            <a href="javascript:void(0);">Register</a>
            <a href="javascript:void(0);">Forgot</a>
        </div>
    </div>
</template>

<script>
    import Message from '../common/message.vue';

    export default {
        data() {
            return {
                login: {
                    account: '',
                    password: ''
                },
                rules: {
                    account: [
                        {required: true, message: '请输入账号', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'}
                    ]
                }
            };
        },
        components: {
            Message
        },
        methods: {
            handleLogon() {
                console.info('go to logon...');
            },
            handleLogin() {
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        this.$http.post('/admin/index/login', JSON.stringify(this.login)).then((response) => {
                            let data = response.data;
                            // success callback
                            if (data.errno == 0) {
                                this.$refs.message.success({
                                    message: '登录成功!'
                                });
                                window.location.href = "/admin/index/index";
                            } else {
                                this.$refs.message.error({
                                    message: data.errmsg
                                });
                            }
                        }, (response) => {
                            // error callback
                            console.error(response);
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
        }
    }
</script>